package com.web.portal.controller.admin;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.gson.Gson;
import com.web.portal.pojo.PortalUser;
import com.web.portal.pojo.Project;
import com.web.portal.pojo.response.ProjectAndProjectParticipant;
import com.web.portal.response.ResponseResult;
import com.web.portal.services.ProjectService;
import com.web.portal.services.UserService;
import com.web.portal.utils.IdWorker;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;

/**
 * 项目
 */
@Slf4j
@RestController
@Api(tags = "项目管理")
@RequestMapping("/admin/project")
public class ProjectAdminApi {
    @Autowired
    private IdWorker idWorker;
    @Autowired
    private ProjectService projectService;

    /**
     * 添加项目
     *
     * @param project
     * @return
     */
    @PostMapping
    public ResponseResult addProject(@RequestBody Project project) {
        project.setProjectCode(idWorker.nextId() + "");
        boolean flag = projectService.save(project);
        if (flag) {
            return ResponseResult.SUCCESS("添加成功");
        } else {
            return ResponseResult.FAILED("添加失败");
        }
    }

    /**
     * 获取 项目
     * <p>
     * 使用的case:修改的时候，获取一下。填充弹窗
     * 不获取也是可以的，从列表里获取数据
     * <p>
     * 权限：管理员权限
     *
     * @param projectId
     * @return
     */
    @GetMapping("{projectId}")
    public ResponseResult getProject(@PathVariable("projectId") String projectId) {
        Project project = projectService.getById(projectId);
        if (project == null) {
            return ResponseResult.FAILED("查找失败");
        }
        return ResponseResult.SUCCESS().setData(project);
    }

    @Autowired
    private Gson gson;

    @Autowired// 阔以表明引用哪个类
    private UserService userService;

    /**
     * 获取 项目
     * <p>
     * 使用的case:修改的时候，获取一下。填充弹窗
     * 不获取也是可以的，从列表里获取数据
     * <p>
     * 权限：管理员权限
     *
     * @param projectId
     * @return
     */
    @GetMapping("project_participant/{projectId}")
    public ResponseResult getProjectAndProjectParticipant(@PathVariable("projectId") String projectId) {
        Project project = projectService.getById(projectId);
        if (project == null) {
            return ResponseResult.FAILED("查找失败");
        }
        String projectJson = gson.toJson(project);
        ProjectAndProjectParticipant participant = gson.fromJson(projectJson, ProjectAndProjectParticipant.class);
        String projectParticipant = project.getProjectParticipant();
        String[] split = projectParticipant.split(",");
        ArrayList<PortalUser> portalUsers = new ArrayList<>();
        for (int i = 0; i < split.length; i++) {
            PortalUser portalUser = userService.selectByUserCodeInfo(split[i]);
            portalUsers.add(portalUser);
        }
        participant.setPortalUser(portalUsers);
        return ResponseResult.SUCCESS().setData(participant);
    }

    /**
     * 获取 项目
     * <p>
     * 使用的case:修改的时候，获取一下。填充弹窗
     * 不获取也是可以的，从列表里获取数据
     * <p>
     * 权限：管理员权限
     *
     * @param projectCode
     * @return
     */
    @GetMapping("find_project/{project_code}")
    public ResponseResult getProjectAndParticipantByProjectCode(@PathVariable("project_code") String projectCode) {
        Project project = projectService.selectByProjectCode(projectCode);
        if (project == null) {
            return ResponseResult.FAILED("查找失败");
        }
        String projectJson = gson.toJson(project);
        ProjectAndProjectParticipant participant = gson.fromJson(projectJson, ProjectAndProjectParticipant.class);
        String projectParticipant = project.getProjectParticipant();
        String[] split = projectParticipant.split(",");
        ArrayList<PortalUser> portalUsers = new ArrayList<>();
        for (int i = 0; i < split.length; i++) {
            PortalUser portalUser = userService.selectByUserCodeInfo(split[i]);
            portalUsers.add(portalUser);
        }
        participant.setPortalUser(portalUsers);
        return ResponseResult.SUCCESS().setData(participant);
    }

    /**
     * 获取 项目
     * <p>
     * 权限：管理员权限
     *
     * @return
     */
    @GetMapping("/list")
    public ResponseResult listProject() {
        return ResponseResult.SUCCESS("获取分类列表成功.")
                .setData(projectService.list());
    }

    //3 查询带分页
    @ApiOperation(value = "分类查询带分页")
    @PostMapping("find_project/{current}/{limit}")
    public ResponseResult findProject(@PathVariable long current,
                                      @PathVariable long limit) {
        //创建page对象，传递当前页，每页记录数
        Page<Project> page = new Page<>(current, limit);
        QueryWrapper<Project> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("create_time");
        //调用方法实现分页查询
        Page<Project> pageLabelInfo = projectService.page(page, wrapper);
        //返回结果
        return ResponseResult.SUCCESS("获取分类列表成功.")
                .setData(pageLabelInfo);
    }

    /**
     * 更新 项目
     *
     * @param project
     * @return
     */
    @PostMapping("updateLabelInfo")
    public ResponseResult updateProject(@RequestBody Project project) {
        boolean flag = projectService.updateById(project);
        if (flag) {
            return ResponseResult.SUCCESS("更改成功");
        } else {
            return ResponseResult.FAILED("更改失败");
        }
    }

    /**
     * 项目
     *
     * @param projectId
     * @return
     */
    @DeleteMapping("/{projectId}")
    public ResponseResult deleteProject(@PathVariable("projectId") String projectId) {
        boolean flag = projectService.removeById(projectId);
        if (flag) {
            return ResponseResult.SUCCESS("删除成功");
        } else {
            return ResponseResult.FAILED("删除失败");
        }
    }
}
